package com.openspring.platform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import javax.swing.SortOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.openspring.framework.service.AbstractGenericService;
import com.openspring.framework.tree.Node;
import com.openspring.platform.dao.DeptDAO;
import com.openspring.platform.dao.UserDAO;
import com.openspring.platform.model.Dept;
import com.openspring.platform.model.Role;
import com.openspring.platform.model.User;
import com.openspring.platform.service.UserService;

@Service("userService")
public class UserServiceImpl extends AbstractGenericService<User, Long> implements UserService {
	
	private UserDAO userDAO;
	
	@Resource(name = "userDAO")
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
		super.setGenericDAO(userDAO);
	}
	
	private DeptDAO deptDAO;
	
	@Resource(name = "deptDAO")
	public void setDeptServie(DeptDAO deptDAO) {
		this.deptDAO = deptDAO;
	}

	@Override
	public Node getDeptUserTree() {
		Node root = new Node();
		root.setText(".");
		Map<Long, Node> dmap = new HashMap<Long, Node>();
		Map<Long, List<Node>> umap = new HashMap<Long, List<Node>>();
		List<Dept> depts = deptDAO.queryAll();
		List<User> users = userDAO.queryAll();
		Iterator<Dept> diter = depts.iterator();
		Iterator<User> uiter = users.iterator();
		while(diter.hasNext()) {
			Node dnode = new Node();
			Dept dept = diter.next();
			dnode.setText(dept.getCnName());
			dnode.setIconCls("dept");
			dnode.setParentId(dept.getParentId());
			dnode.setCustomId(dept.getId());
			dmap.put(dept.getId(), dnode);
		}
		while(uiter.hasNext()) {
			Node unode = new Node();
			User user = uiter.next();
			BeanUtils.copyProperties(user, unode);
			unode.setText(user.getUserName());
			unode.setChecked(false);
			unode.setIconCls("user");
			Long deptId = user.getDeptId();
			List<Node> list = umap.get(deptId);
			if(list == null) {
				list = new ArrayList<Node>();
				list.add(unode);
			} else {
				list.add(unode);
			}
			umap.put(deptId, list);
		}
		Set<Map.Entry<Long, Node>> entrySet = dmap.entrySet();
		Iterator<Map.Entry<Long, Node>> iter = entrySet.iterator();
		while(iter.hasNext()) {
			Node node = iter.next().getValue();
			List<Node> list = umap.get(node.getCustomId());
			if(list != null) {
				node.getChildren().addAll(list);
			}
			if(node.getParentId() == 0) {
				if(!node.getChildren().isEmpty())
					root.getChildren().add(node);
			} else {
				Node parentNode = dmap.get(node.getParentId());
				if(parentNode != null && !node.getChildren().isEmpty())
					parentNode.getChildren().add(node);
			}
		}
		root.sort(SortOrder.ASCENDING);
		return root;
	}

	@Override
	public User get(String loginName, String password) {
		return userDAO.getUser(loginName, password);
	}

	@Override
	public List<Role> getRoles(Long id) {
		return userDAO.getRoles(id);
	}

	@Override
	public void modifyPassword(Long id, String password) {
		userDAO.updatePassword(id, password);
	}
}
